<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" th:with="title='友链管理',active='link'">
<header th:replace="back/header::headerFragment(${title},${active})"></header>
<head>
    <meta th:name="_csrf" th:content="${_csrf.token}"/>
    <!-- 默认的header name是X-CSRF-TOKEN -->
    <meta th:name="_csrf_header" th:content="${_csrf.headerName}"/>
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"
          integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">

    <script src="https://cdn.bootcss.com/jquery/2.2.2/jquery.js"></script>
    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"
            integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd"
            crossorigin="anonymous"></script>
</head>
<style>
    .topbar {
        height: 70px;
    }
</style>

<body class="fixed-left">

<div id="wrapper">
    <div th:replace="back/header::header-body"></div>
    <div class="content-page">
        <div class="content">
            <div class="container">
                <div class="row">
                    <!--                    <div class="col-sm-12"></div>-->
                    <div class="col-sm-12">
                        <h4 class="page-title">友链管理</h4>
                        <a class="btn btn-info" data-toggle="modal" data-target="#myModal2">添加友链</a>
                    </div>


                    <div class="col-md-12">

                        <div class="table-responsive">
                            <table th:if="${links!=null and links.size()>0}" class="table table-striped table-bordered">
                                <thead>
                                <tr>
                                    <th>id</th>
                                    <th>标题</th>
                                    <th>链接</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                <tr th:each="link:${links}">
                                    <td th:text="${link.getId()}"></td>
                                    <td th:text="${link.getTitle()}"></td>
                                    <td th:text="${link.getLink()}"></td>

                                    <td>
                                        <a href="#"
                                           data-toggle="modal" data-target="#myModal"
                                           th:onclick="changeLink([[${link.getId()}]],[[${link.getTitle()}]],[[${link.getLink()}]])"
                                           class="btn btn-primary btn-sm waves-effect waves-light m-b-5">
                                            <i class="fa fa-edit"></i><span>编辑</span></a>

                                        <a th:onclick="f1(this,[[${link.getId()}]])" href="#"
                                           class="btn btn-danger btn-sm waves-effect waves-light m-b-5">
                                            <i class="fa fa-trash-o"></i> <span>删除</span></a>
                                    </td>

                                </tr>
                                </tbody>
                            </table>
                        </div>


                    </div>


                </div>
                <div th:replace="back/footer :: footer-content"></div>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">友链编辑</h4>
            </div>
            <form method="post" th:action="@{/link/updateLink}" autocomplete="off">
                <div class="modal-body">
                    <input type="hidden" name="id" id="lid">
                    <br/>
                    标题：<input type="text" name="title" id="ltitle" required>
                    <br/>
                    链接：<input type="text" name="link" id="link" required>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="submit" class="btn btn-primary">确认修改</button>
                </div>
            </form>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>


<div class="modal fade" id="myModal2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel2">添加友链</h4>
            </div>
            <form method="post" th:action="@{/link/addLink}" autocomplete="off">
                <div class="modal-body">
                    标题：<input type="text" name="title" required>
                    <br/>
                    链接：<input type="text" name="link" required>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="submit" class="btn btn-primary">确认添加</button>
                </div>
            </form>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>


<div th:replace="back/footer :: footer"></div>

</body>

<script>

    function changeLink(lid, ltitle, link) {
        document.getElementById('lid').value = lid;
        document.getElementById('ltitle').value = ltitle;
        document.getElementById('link').value = link;
    }

    function f1(obj, id) {
        var res = confirm("你是否要删除" + id + "号友链");
        if (res == true) {
            obj.href = '/link/deleteLink?id=' + id;
        } else {
        }

    }


</script>


</html>